package info.openlvb.openface.client.handler;

import info.openlvb.openface.client.base.person.ModifyAccountWindow;
import info.openlvb.openface.client.service.PropertiesService;
import info.openlvb.openface.client.service.PropertiesServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;

public class AddMailAddressListener extends ButtonListenerAdapter{

	private ModifyAccountWindow maw;
	private final PropertiesServiceAsync propertiesService = GWT.create(PropertiesService.class);

	public AddMailAddressListener(ModifyAccountWindow maw){
		this.maw = maw;

	}
	public void onClick(Button button, EventObject e) {
		if(button.getText().equals("Créer une adresse mail")){
			propertiesService.getDomain(new AsyncCallback<String>() {
				public void onSuccess(String result) {
					maw.getMail().setValue(maw.getPseudo().getValueAsString()+"@"+result);
				}
				public void onFailure(Throwable caught) {
					MessageBox.alert("probleme de backend");
				}
			});
			button.setText("Supprimer l'adresse mail");
		}else{
			maw.getMail().setValue("");
			button.setText("Créer une adresse mail");
		}
	}
}
